package com.poseidon.proxy.staticproxy;

import com.poseidon.proxy.vo.Demo;
import com.poseidon.proxy.vo.DemoImpl;

public class DemoStaticProxy implements Demo, BaseProxy {

	private DemoImpl demoImpl;

	public static DemoStaticProxy getInstance() {
		return new DemoStaticProxy(new DemoImpl());
	}

	private DemoStaticProxy(DemoImpl dempImpl) {
		super();
		this.demoImpl = dempImpl;
	}

	private DemoStaticProxy() {
	}

	public void add(String param) {
		try {
			before();
			this.demoImpl.add(param);
			after();
		} catch (Throwable e) {
			exceptionThrow();
		} finally {
			last();
		}
	}

	public void delete(int id) {
		try {
			before();
			this.demoImpl.delete(id);
			after();
		} catch (Throwable e) {
			exceptionThrow();
		} finally {
			last();
		}
	}

	public void update(int id, String param1, String param2) {
		try {
			before();
			this.demoImpl.update(id, param1, param2);
			after();
		} catch (Throwable e) {
			exceptionThrow();
		} finally {
			last();
		}
	}

	public String select(int id) {
		Object obj = null;
		try {
			before();
			obj = this.demoImpl.select(id);
			after();
		} catch (Throwable e) {
			exceptionThrow();
		} finally {
			last();
		}
		return String.valueOf(obj);
	}

	public void before() {
		System.out.println("方法调用前执行：doSomething");
	}

	public void after() {
		System.out.println("方法调用后执行：doSomething");
	}

	public void last() {
		System.out.println("方法调用完成后：doSomething");
	}

	public void exceptionThrow() {
		System.out.println("方法遇到异常：doSomething");
	}
}